var util = require('../utils/util.js');
Page({
  data: {
  
    guest: {
      name: '',
      idcard:'',
      phone: '',
      email:'',

    },
    year:'',

    gender: ['男', '女'],
    gender_value: '',

    politics:['中共党员','中共预备党员','共青团员','群众'],
    politics_value: '',

  },
  onLoad:function(){
    var nowdate = util.formatTime(new Date());
    // console.log(nowdate.substring(0, 4))
    this.setData({
      year: nowdate.substring(0, 4)
    })
  },

    // // 输入框获取焦点事件监听
    // inputFocus: function (e) {
    //   var id = e.currentTarget.id;
    //   var string = id + '_focus';
    //   this.setData({
    //     [string]: true
    //   })
    // },


    // name输入框失去焦点监听
  nameChange: function (e) {
    this.setData({
      name_focus: false
    })

    // 校验姓名
    var name = e.detail.value;
    this.checkName(name);
  },

  // 用正则表达式校验姓名
  checkName: function (name) {
    var reg = /^[\u4E00-\u9FA5A-Za-z]+$/;
    return this.check(reg, name, '输入的姓名有误');
  },
 

  // 用正则表达式校验
  check: function (reg, data, errorMsg) {
    if (data == '' || !reg.test(data)) { // 输入为空或者非法时，提醒用户
      wx.showToast({
        title: errorMsg,
        icon: 'none',
        duration: 3000
      })
      return false;
    }
    return true;
  },
  getname:function(e){
    this.data.guest.name = e.detail.value
    this.setData({
      guest:this.data.guest
    })
  },
  getidcard:function(e){
    this.data.guest.idcard = e.detail.value
    this.setData({
      guest:this.data.guest
    })
  },
  getphone:function(e){
    this.data.guest.phone = e.detail.value
    this.setData({
      guest:this.data.guest
    })
  },
  getemail:function(e){
    this.data.guest.email = e.detail.value
    this.setData({
      guest:this.data.guest
    })
  },
  
  //性别选择
  bindPickerGenderChange(e) {
    this.setData({
      gender_value: e.detail.value,
    });
  },
  //政治面貌
  bindPickerPoliticsChange(e) {
    this.setData({
      politics_value: e.detail.value,
    });
    
  },


  //用正则表达式校验身份证
  checkIdcard:function(idcard) {
    var reg = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
    return this.check(reg, idcard, '输入的身份证号码有误');
  },


  // 用正则表达式校验电话
  checkPhone: function (phone) {
    var reg = /^(((13)|(15)|(17)|(18))\d{9})$/;
    return this.check(reg, phone, '输入的电话有误');
  },


  // 用正则表达式校验邮箱
  checkEmail: function (email) {
    var reg = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/;
    return this.check(reg, email, '输入的电子邮箱有误');
  },


  //跳转去志愿者个人页面
  volunteer:function(e){
    if (!this.checkName(this.data.guest.name)) {
      wx.showToast({
        title: '姓名输入有误！',
        icon: 'none'
      })
      return;
    }
    if (!this.checkPhone(this.data.guest.phone)) {
      wx.showToast({
        title: '电话输入有误！',
        icon: 'none'
      })
      return;
    }
    if (!this.checkIdcard(this.data.guest.idcard)) {
      wx.showToast({
        title: '身份证号码输入有误！',
        icon: 'none'
      })
      return;
    }
    if (!this.checkEmail(this.data.guest.email)) {
      wx.showToast({
        title: '电子邮箱输入有误！',
        icon: 'none'
      })
      return;
    }
    console.log(wx.getStorageSync('openid'))
    var id = wx.getStorageSync('openid')
    var guest = this.data.guest
    var name = guest.name
    var gender = this.data.gender[this.data.gender_value]
    var age = (this.data.year-this.data.guest.idcard.substring(6,10))
    var politic = this.data.politics[this.data.politics_value]
    var idcard = guest.idcard
    var phone = guest.phone
    var email = guest.email
    var formData={
        volid:id,
        volname :name,
        age:age,
        gender :gender,
        politic :politic,
        idcard: idcard,
        phone : phone,
        email : email
    }
   
    console.log(JSON.stringify(formData))
    wx.request({
      url: 'http://127.0.0.1:8082/demo1/superadmin/insertvolunteer',
      data:JSON.stringify(formData),
      method:'POST',
      header:{
        'Content-Type': 'application/json'
      },
      success:(res)=>{
        console.log(res)
        wx.setStorageSync('identity', 'vol')
        wx.switchTab({
          url: '/pages/person/person',
        })
      }
    })
   
   
  }
});
